import Cookies from 'js-cookie'
// import router from '@/router'
import iView from 'iview'
import Vue from 'vue'
Vue.use(iView)

const app = {
  state: {
    ready: false,
    sidebar: {
      opened: !+Cookies.get('sidebarStatus'),
      theme: 'dark',
      menus: [],
      minOpen: false // 小屏时菜单状态
    },
    router: {
      currentPageName: 'Index',
      openedMenuNameList: [],
      openedPageNameList: [],
      tabList: []
    },
    searchState: {
      show: false,
      params: ''
    }
  },
  mutations: {
    SET_READY: (state, status) => {
      state.ready = status
    },
    TOGGLE_SIDEBAR: state => {
      let winWidth = document.documentElement.clientWidth
      if (winWidth <= 600) {
        state.sidebar.minOpen = !state.sidebar.minOpen
        state.sidebar.opened = false
        Cookies.set('sidebarStatus', 0)
        return
      } else {
        if (state.sidebar.opened) {
          Cookies.set('sidebarStatus', 1)
        } else {
          Cookies.set('sidebarStatus', 0)
        }
        state.sidebar.opened = !state.sidebar.opened
      }
    },
    CLOSE_SLIDEBAR: state => {
      Cookies.set('sidebarStatus', 0)
      state.sidebar.opened = false
      state.sidebar.minOpen = false
    },
    OPEN_SLIDEBAR: state => {
      Cookies.set('sidebarStatus', 1)
      state.sidebar.opened = true
    },
    SEARCH_FILTER: (state, payload) => {
      if (!payload.params.meta.search) { // 路由元信息设置是否有搜索模块
        iView.Message.error('该页不提供搜索!')
        return
      } else {
        Object.assign(state.searchState, {
          show: !state.searchState.show
        }, payload)
      }
    },
    SET_MENUS: (state, payload) => {
      state.sidebar.menus = payload
    },
    SET_CURRENT_PATH: (state, payload) => {
      let currentPath = []
      let openedMenuList = state.sidebar.menus.filter(menu => {
        if (menu.children.length === 0) {
          return menu.name === payload
        } else {
          return menu.children.some((child) => {
            if (child.name === payload) {
              currentPath.push(child)
            }

            return child.name === payload
          })
        }
      })

      state.router.currentPageName = payload
      state.router.openedMenuNameList = openedMenuList.map(item => {
        return item.name
      })

      if (openedMenuList[0] && openedMenuList[0].name !== 'Index') {
        let currentNode = {
          title: '首页',
          name: 'Index'
        }
        currentPath.push(currentNode)
      }

      currentPath.reverse().map(item => {
        if (state.router.openedPageNameList.indexOf(item.name) === -1) {
          state.router.openedPageNameList.push(item.name)
          state.router.tabList.push({
            name: item.name,
            title: item.title
          })
        }
      })

    },
    REMOVE_TAB: (state, payload) => {
      let idx = state.router.openedPageNameList.indexOf(payload)
      state.router.openedPageNameList.splice(idx, 1)
      state.router.tabList.splice(idx, 1)
    }
  },
  actions: {
    setReady: ({
      commit
    }, status) => {
      commit('SET_READY', status)
    },
    ToggleSideBar: ({
      commit
    }) => {
      commit('TOGGLE_SIDEBAR')
    },
    searchFilter: ({
      commit
    }, payload) => {
      commit('SEARCH_FILTER', payload)
    },
    setMenus: ({
      commit
    }, payload) => {
      commit('SET_MENUS', payload)
    },
    setCurrentPath: ({
      commit
    }, payload) => {
      commit('SET_CURRENT_PATH', payload)
    },
    removeTab: ({commit}, payload) => {
      commit('REMOVE_TAB', payload)
    }
  }
}

export default app
